package ui.handlers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import ui.PayTransactionScreen;
import ui.ViewTransactionScreen;

public class PayHandler extends BaseHandler implements ActionListener {

	int transactionID;
	ViewTransactionScreen viewTranScrn = null;
	PayTransactionScreen payTranScrn = null;
	
	public PayHandler(int transactionID, ViewTransactionScreen vts)
	{ this.transactionID = transactionID; viewTranScrn = vts; }
	
	public PayHandler(int transactionID, PayTransactionScreen pts)
	{ this.transactionID = transactionID; payTranScrn = pts; }
	
	public void actionPerformed(ActionEvent e)
	{
		if (viewTranScrn != null)
		{ new PayTransactionScreen(transactionID); }
		
		if (payTranScrn != null)
		{
			if (e.getSource() == payTranScrn.getPayButton())
			{ payTransaction(); }
			else if (e.getSource() == payTranScrn.getCancelButton())
			{ close(payTranScrn.getMainFrame()); }
		}
	}
	
	private void payTransaction()
	{
		if (payTranScrn.getCashRButton().isSelected())
		{
			String cashAmount = payTranScrn.getCashText();
			
			if (cashAmount.equals(""))
			{ return; }
			
			if (cashAmount.charAt(0) != '$')
			{ cashAmount = "$"+ cashAmount; }
			
			try {
				payTranScrn.getStore().renderCashPayment(transactionID, cashAmount);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (payTranScrn.getCreditRButton().isSelected())
		{
			if (payTranScrn.getCreditText().equals("") || payTranScrn.getExpDateText().equals(""))
			{ return; }
			
			try {
				payTranScrn.getStore().renderCreditPayment(transactionID, payTranScrn.getCreditText(), payTranScrn.getExpDateText());
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		close(payTranScrn.getMainFrame());
	}
}